<!--
 * @Description: 你的代码我的心
 * @Author: GGB
 * @Date: 2021-12-24 14:08:47
 * @LastEditors: GGB
 * @LastEditTime: 2022-01-26 09:38:18
-->
<template>
  <BasicModal v-bind="$attrs" @ok="handleOk" @register="register" title="添加店铺">
    <!-- 表单信息 -->
    <BasicForm class="p-4" @register="formRegister" />
  </BasicModal>
</template>
<script lang="ts" setup>
  import { formSchemas } from './modal.data';
  import { addStore } from '/@/api/store';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { StoreParams, StoreFormParams } from '/@/api/store/model/storeModel';
  let reload = () => {};
  const [formRegister, { getFieldsValue, resetFields }] = useForm({
    schemas: formSchemas,
    layout: 'vertical',
    showActionButtonGroup: false,
    actionColOptions: {
      span: 24,
    },
  });
  const [register, { closeModal, changeOkLoading }] = useModalInner((fn) => (reload = fn));
  // 更改信息
  const handleOk = () => {
    let value = getFieldsValue() as StoreFormParams;
    let param = { form: value } as StoreParams;
    changeOkLoading(true);
    const { createMessage } = useMessage();
    addStore(param)
      .then(({ code, message }) => {
        code === 0 && createMessage.success(message);
        reload();
        closeModal();
      })
      .catch(() => {
        createMessage.error('添加失败');
      })
      .finally(() => {
        changeOkLoading(false);
        resetFields();
      });
  };
</script>

<style lang="less" scoped>
  .container {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    min-height: 200px;
  }
</style>
